package it.afs.worklog.application.form.admin;

import it.afs.worklog.User;

import java.util.List;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.PropertyModel;

/**
 * @author Fanta
 */
public class UtentiPage extends AdminPage
{
	public UtentiPage()
	{
		super();
		
		addComponents();
	}
	
	@Override
	protected void addComponents()
	{
		super.addComponents();
		
		add(getHomePageLink());
		
		LoadableDetachableModel<List<User>> utentiModel = new LoadableDetachableModel<List<User>>()
		{
			private static final long serialVersionUID = 8918091909483318743L;

			@Override
			protected List<User> load()
			{
				return getFactory().getDao().getUtenti();
			}
		};
		
		ListView<User> clienti = new ListView<User>("clienti", utentiModel)
		{
			private static final long serialVersionUID = 4783074671769570018L;

			@Override
			protected void populateItem(ListItem<User> item)
			{
				item.add(new Label("username", new PropertyModel<String>(item.getModel(), "username")));
				item.add(new Label("nome", new PropertyModel<String>(item.getModel(), "nome")));
				item.add(new Label("livello", new PropertyModel<Integer>(item.getModel(), "livello")));
				item.add(new Label("cf", new PropertyModel<String>(item.getModel(), "cf")));
				item.add(new Label("qualifica", new PropertyModel<String>(item.getModel(), "qualifica")));
				item.add(new Label("contratto", new PropertyModel<String>(item.getModel(), "contratto")));
				item.add(new Label("contatto", new PropertyModel<String>(item.getModel(), "contatto")));
				item.add(new Label("costo", new PropertyModel<String>(item.getModel(), "costo")));
				
				item.add(new Link<String>("edit", new PropertyModel<String>(item.getModel(), "id"))
				{
					private static final long serialVersionUID = -3391242280760422731L;

					@Override
					public void onClick()
					{
						setResponsePage(UtenteFormPage.class, new PageParameters("id=" + getModelObject()));
					}
				});
			}
		};
		add(clienti);
		
		add(new Link<Void>("add")
		{
			private static final long serialVersionUID = -3099564635631488168L;

			@Override
			public void onClick()
			{
				setResponsePage(UtenteFormPage.class);
			}
		});
	}
}